import Watcher from "./observe/Watcher"
import { patch } from "./vnode/patch"

export function mounetCompoent(vm,el) {
    // 页面挂载之前调用beforeMount生命周期
    callHook(vm,'beforeMount')
    // vm_render()将ast中的render变为vnode
    // vm._update将vnode变为真实dom并放到页面上去
    let updataComponent = (type) => {
        vm._update(vm._render())  
    }
    new Watcher(vm,updataComponent,()=>{
        callHook(vm,'updated')
    },true)
    // 页面挂载之前调用mounted生命周期
     callHook(vm,'mounted')
}   


export function lifecycleMixin(Vue) {
    Vue.prototype._update = function (vnode) {
        let vm = this
        vm.$el =  patch(vm.$el,vnode)
    }
}

// 生命周期的调用

export function callHook(vm,hook) {
    const handlers = vm.$options[hook]
    if(handlers){
        for (let i = 0; i < handlers.length; i++) {
            handlers[i].call(vm) // 改变生命周期中的this问题
        }
    }
}